<template>
  <div>
    <div class="form-content" v-if="statusImplementation" :key="1">
      <p>6.0负责实施方案</p>
      <el-form ref="form" class="form-input" :model="formData" label-width="120px" label-position="right" :rules="ruleData">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上传方案">
              <UploadFile @getfiles="getfiles1" :showFiles="showFiles1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检是否完成">
              <el-switch v-model="formData.complete"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row><span class="word">下一流程:</span></el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="图纸导PDF">
              <el-input v-model="formData.nextAuthor1" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预算导PDF">
              <el-input v-model="formData.nextAuthor2" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="参数表导PDF">
              <el-input v-model="formData.nextAuthor3" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <div class="tip">
            注：实施方案不要放设计任务书、委托书、勘察报告、检测报告、参数表，不能出现品牌型号、专家名称，自留表可以放、封面不放设计单位公章！
          </div>
        </el-row>
        <div class="btn-option">
          <el-button type="primary" @click="submit">提交到下一流程</el-button>
          <el-button @click="cancel">取消</el-button>
          <el-button type="warning" @click="back">退回到上一流程</el-button>
        </div>
      </el-form>
    </div>

    <div class="form-content" v-else :key="2">
      <p>6.0负责实施方案</p>
      <el-form class="form-input" :model="formData2" label-width="120px" label-position="right">
        <el-row>
          <el-col :span="12">
            <el-form-item label="下载方案">
              <div v-if="formData2.file1.length > 0">
                <div v-for="item in formData2.file1" :key="item.id" @click="downFile1(item.id)" class="filediv">{{item.fileName}}</div>
              </div>
              <div v-else>未上传文件</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检是否完成">
              <el-switch v-model="formData2.complete" disabled></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row><span class="word">下一流程:</span></el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="图纸导PDF">
              <el-input v-model="formData.nextAuthor1" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预算导PDF">
              <el-input v-model="formData.nextAuthor2" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="参数表导PDF">
              <el-input v-model="formData.nextAuthor3" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <div class="tip">
            注：实施方案不要放设计任务书、委托书、勘察报告、检测报告、参数表，不能出现品牌型号、专家名称，自留表可以放、封面不放设计单位公章！
          </div>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
import { EmbodimentCreate } from '@/api/api'
import { getName, getAccount, getFileId, downfile, getAccountBack, nextNodeArr } from '@/unils/utils'
import Bus from '@/plugins/bus'
import swal from 'sweetalert'
import _ from 'underscore'
import uploadFile from '../flow/upload'
export default {
  props: {
    statusImplementation: {
      type: Boolean,
      defalut: true
    }
  },
  components: {
    UploadFile: uploadFile,
  },
  data () {
    return {
      orunid: '',
      formData: {
        complete: false,
        nextAuthor1: '',
        nextAuthor2: '',
        nextAuthor3: '',
        file1: []
      },
      ruleData: {},
      formData2: {
        complete: false,
        addName: '',
        file1: [],
      },
      formData3: {
        nextAuthor1: '',
        nextAuthor2: '',
        nextAuthor3: ''
      },
      backNextAuthor: '',
      //
      showFiles1: [],
      fileList2_1: [],
    }
  },
  mounted () {
    this.fun()
  },
  beforeDestroy () {
    Bus.$off('res')
  },
  methods: {
    // 文件操作
    getfiles1 (e) {
      this.formData.file1 = e[0]
      this.fileList2_1 = e[1]
    },
    downFile1 (id) {
      downfile(id)
    },
    submit: _.debounce(async function () {
      await this.$refs.form.validate(async (valid) => {
        if (!valid) return false
        let msg = '是否确认提交!'
        // if (this.formData.file1.length == 0) {
        //   msg = '检测到有文件没提交，是否确认提交!'
        // }
        await this.confirm(msg, async () => {
          let file1Obj = await getFileId(this, this.formData.file1, 1, 6)
          let nodeTowardsInfo = [
            { currentAuthor: this.$store.state.jobAccount, nextNodeID: 'T10008', nextAuthor: this.formData.nextAuthor1, nextNodeName: '7.0图纸导PDF', nextAuthorCN: '', currentNodeID: 'T10007', currentNodeName: '6.0负责实施方案' },
            { currentAuthor: this.$store.state.jobAccount, nextNodeID: 'T10009', nextAuthor: this.formData.nextAuthor2, nextNodeName: '8.0预算导PDF', nextAuthorCN: '', currentNodeID: 'T10007', currentNodeName: '6.0负责实施方案' },
            { currentAuthor: this.$store.state.jobAccount, nextNodeID: 'T10010', nextAuthor: this.formData.nextAuthor3, nextNodeName: '9.0参数表导PDF', nextAuthorCN: '', currentNodeID: 'T10007', currentNodeName: '6.0负责实施方案' }
          ]
          let params = {
            radio5: this.formData.complete,
            orunid: this.orunid,
            status: 'current',
            type: 2,
            addName: this.formData2.addName,
            currentNodeName: '6.0负责实施方案',
            currentNodeID: 'T10007',
            nextNodeID: 'T10008, T10009, T10010',
            nodeTowardsInfo: JSON.stringify(nodeTowardsInfo),
            planFile: JSON.stringify(file1Obj.concat(this.fileList2_1))
          }
          // console.log(params)
          EmbodimentCreate(params)
            .then((res) => {
              // console.log(res)
              if (res.code == 200) {
                this.alertok('提交成功', () => {
                  this.$router.go(-1)
                })
              } else {
                swal('错误', res.message || '网络连接错误', 'warning')
              }
            })
            .catch((err) => {
              swal('错误', '网络连接错误', 'error')
            })
        })
      })
    }),
    cancel () {
      this.$router.go(-1)
    },
    async back () {
      await this.confirm('是否确认退回', async () => {
        let file1Obj = await getFileId(this, this.formData.file1, 1, 6)
        let params = {
          radio5: this.formData.complete,
          orunid: this.orunid,
          status: 'current',
          type: 0,
          addName: this.formData2.addName,
          currentNodeName: '6.0负责实施方案',
          currentNodeID: 'T10007',
          nextNodeID: 'T10006',
          nodeTowardsInfo: nextNodeArr(this.$store.state.jobAccount, [this.backNextAuthor], 'T10006', '5.0负责参数表', 'T10007', '6.0负责实施方案'),
          planFile: JSON.stringify(file1Obj.concat(this.fileList2_1))
        }
        EmbodimentCreate(params)
          .then((res) => {
            // console.log(res)
            if (res.code == 200) {
              this.alertok('提交成功', () => {
                this.$router.go(-1)
              })
            } else {
              swal('错误', res.message || '网络连接错误', 'warning')
            }
          })
          .catch((err) => {
            swal('错误', '网络连接错误', 'error')
          })
      })
    },
    fun () {
      Bus.$on('res', res => {
        this.orunid = res.data.orunid
        // 显示之前流程的提交人员
        this.formData.nextAuthor1 = getAccountBack(res.data.nodeTowardsInfo, 'T10003')
        this.formData.nextAuthor2 = getAccountBack(res.data.nodeTowardsInfo, 'T10005')
        this.formData.nextAuthor3 = getAccountBack(res.data.nodeTowardsInfo, 'T10006')

        this.formData2.complete = JSON.parse(res.data.radio5)
        this.formData2.addName = res.data.addName
        if (res.data.planFile != null && JSON.parse(res.data.planFile).length > 0) {
          this.formData2.file1 = JSON.parse(res.data.planFile)
          this.showFiles1 = JSON.parse(res.data.planFile)
          this.fileList2_1 = JSON.parse(res.data.planFile)
        }
        // this.formData3.nextAuthor1 = getAccount(res.data.nodeTowardsInfo, 'T10003')
        // this.formData3.nextAuthor2 = getAccount(res.data.nodeTowardsInfo, 'T10005')
        // this.formData3.nextAuthor3 = getAccount(res.data.nodeTowardsInfo, 'T10006')

        // 获取要退回到的上一流程人
        this.backNextAuthor = getAccountBack(res.data.nodeTowardsInfo, 'T10006')
      })
    }
  },
}
</script>
<style lang="less" scoped>
.form-content {
  font-size: 30px;
  p {
    text-align: center;
    margin-bottom: 40px;
  }
  .form-input {
    width: 70%;
    // background-color: blue;
    margin: auto;
    .btn-option {
      display: flex;
      justify-content: flex-end;
    }
  }
  .tip {
    font-size: 12px;
    color: rgb(245, 108, 108);
    margin-bottom: 10px;
  }
}
.word {
  font-size: 15px;
  margin-left: 40px;
}
</style>